#include "zygplayer.h"

QMap<QString,QMediaPlayer*>* ZygPlayer::nameAndPlayerMap = new QMap<QString,QMediaPlayer*>;

ZygPlayer::ZygPlayer(){

}

ZygPlayer::~ZygPlayer(){
    QList<QString> keyList = this->nameAndPlayerMap->keys();
    //清理value
    for(int i = 0; i < keyList.size(); i++){
        delete ZygPlayer::nameAndPlayerMap->value(keyList.at(i));
    }
    ZygPlayer::nameAndPlayerMap->clear();
}

void ZygPlayer::play(QString playerName){
    if(!ZygPlayer::nameAndPlayerMap->contains(playerName)){
        QMediaPlayer *player = new QMediaPlayer;
        if(playerName.startsWith("qrc:")){
            player->setMedia(QUrl(playerName));
        }else{
            player->setMedia(QUrl::fromLocalFile(playerName));
        }
        player->setVolume(50);
        ZygPlayer::nameAndPlayerMap->insert(playerName,player);
    }
    ZygPlayer::nameAndPlayerMap->value(playerName)->play();
}

void ZygPlayer::play(){
    ZygPlayer:;play("qrc:/resource/sounds/zyg_come_or_go_off_work.mp3");
}
